const { execSync } = require('child_process');
const fs = require('fs-extra');

function isReleaseBranch() {
    // 获取当前分支名称
    const branchName = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
    return branchName === 'release';
}
function main() {
    const branchToMerge = process.argv[2];
    if (isReleaseBranch()) {
        console.log('\x1b[32m%s\x1b[0m', '执行 merge-branch 脚本，暂存 src/api 目录');
        fs.copySync('src/api', 'temp_src_api');
        try {
            execSync(`git merge --no-commit --no-ff --no-log ${branchToMerge}`, {
                stdio: 'inherit',
            });
        } catch (error) {
        } finally {
            fs.removeSync('src/api');
            // 恢复 src/api 目录
            fs.copySync('temp_src_api', 'src/api');
            // 删除 temp_src_api
            fs.removeSync('temp_src_api');
            console.log('\x1b[32m%s\x1b[0m', '恢复 src/api 目录成功');
        }
    } else {
        console.log('非 release 分支，直接 merge');
        execSync(`git merge --no-commit --no-ff --no-log ${branchToMerge}`, {
            stdio: 'inherit',
        });
    }
}

main();
